<?php

include 'DBconn.php';

class EnquiryDetails {

    public function enquiryListPopup() {
	
        $query = "Select enq_serialnumber,enq_code,	total_qty,	enq_remarks from enquiry_header ";
        $result = mysql_query($query) or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($result)) 
		{
            $temp = new VoEnquiryPopupList;
			$temp->enq_serialnumber = (int) $row->enq_serialnumber;
            $temp->enq_code = $row->enq_code;
			$temp->total_qty = $row->total_qty;
            $temp->enq_remarks = $row->enq_remarks;
          
            $temp->isSelected = false;
            $ret[] = $temp;
        }

        return $ret;
    }

    public function getMailDetails() {
        $temp = new VoEnquiryEmail();
        $temp->baseRemarksEmail = "Sample Remarks";
        $temp->enq_noEmail = "Sample ENQ.NO";
        $temp->pro_nameEmail[] = '';
        $temp->stamp_noEmail[] = '';
        $temp->prat_noEmail[] = '';
        $temp->model_noEmail[] = '';
        $temp->quantityEmail[] = '';
        $temp->remarksEmail[] = '';
        $temp->supplierEmail[] = '';
        return $temp;
    }

    public function SendMail($VObject) {
        $from = "prabhu@360i.in";
        $subject = "Product Enquiry";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= "From: $from";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $headers .="Content-Type: text/css;";
        $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        $message.='<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
        $message.='<head><meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />';
        $message.='<meta name="author" content="" />';
        $message.='<title>Equiry</title>';
        $message.='<style type="text/css">table, td, th{border:1px solid red;text-align:center;}';
        $message.='th{background-color:blue;color:white;}</style>';
        $message.='</head>';
        $message.='<body>';
        $message.='<table>';
        $message.='<th>Product Name</th><th>Part No</th><th>stamping no</th><th>Other Replacement Parts</th><th>Quantity</th><th>Remarks</th>';
        foreach ($VObject->pro_nameEmail as $r => $value) {
            $part_no = mysql_real_escape_string($VObject->prat_noEmail[$r]);
            $stamp_no = mysql_real_escape_string($VObject->stamp_noEmail[$r]);
            $model_no = mysql_real_escape_string($VObject->model_noEmail[$r]);
            $pro_qty = mysql_real_escape_string($VObject->quantityEmail[$r]);
            $pro_remarks = mysql_real_escape_string($VObject->remarksEmail[$r]);
            $message.='<tr><td>';
            $message.="$value";
            $message.='</td><td>';
            $message.="$part_no";
            $message.='</td><td>';
            $message.="$stamp_no";
            $message.='</td><td>';
            $message.="$model_no";
            $message.='</td><td>';
            $message.="$pro_qty";
            $message.='</td><td>';
            $message.="$pro_remarks";
            $message.='</td><td>';
            $message.='</td><td></tr>';
        }
        $message.='</table>';
        $message.='Enquiry Remarks:';
        $message.=$VObject->baseRemarksEmail;
        $message.='</body></html>';
        foreach ($VObject->supplierEmail as $r => $to) {
            @mail($to, $subject, $message, $headers);
        }
        return "true";
    }

    public function EnquiryAutoCode() {
        $query = "select enq_code from enquiry_header order by enq_serialnumber  desc limit 1 ";
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result);
        if ($row[0] != "") {
            $str = substr($row[0], 0, 3);
            $str2 = substr($row[0], 3, 4);
            ++$str2 . "<br>";
            $str3 = $str . $str2;
        } else {
            $str3 = "ENQ1001";
        }
        return $str3;
    }

    public function getEnquiryByCode($enq_serialnumber) {
        $query = "select * from enquiry_header where enq_serialnumber='$enq_serialnumber' and activestatus=1";
         $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoEnquiryHeader();
            $temp->enq_serialnumber = (int) $row->enq_serialnumber;
            $temp->enq_code = $row->enq_code;
			$temp->total_amount = (double) $row->total_amount;
            $temp->total_qty = (double) $row->total_qty;
			$temp->currency_serialnumber_fk = (int) $row->currency_serialnumber_fk;
			$temp->current_rate = (double) $row->current_rate;
			$temp->enq_date = date("d-m-Y", strtotime($row->enq_date));
            $temp->enq_remarks = $row->enq_remarks;
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
            $query1 = "select enq_sup_serialnumber,supplier_serialnumber_fk from enquiry_supplier where enq_serialnumber_fk='$row->enq_serialnumber'and activestatus=1";
            $s1 = mysql_query($query1);
            while ($row1 = mysql_fetch_object($s1)) {
                $temp->enq_sup_serialnumber[] = (int) $row1->enq_sup_serialnumber;
                $temp->supplier_serialnumber_fk[] = (int) $row1->supplier_serialnumber_fk;
            }
            $query2 = "select ed.enq_detserialnumber,ed.pro_serialnumber_fk,ed.unit_price,ed.pro_total_price,ed.pro_qty,ed.pro_remarks,ph.pro_name,ph.part_number,ph.stamp_number,
			ph.pro_description,ph.model_no from enquiry_detail as ed LEFT JOIN product_header as ph ON ph.serialnumber=ed.pro_serialnumber_fk and ph.activestatus=1 
			where ed.enq_serialnumber_fk='$row->enq_serialnumber' and ed.activestatus=1";
            $s2 = mysql_query($query2);
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->enq_detserialnumber[] = (int) $row2->enq_detserialnumber;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
                $temp->pro_qty[] = (double) $row2->pro_qty;
				$temp->unit_price[] = (double) $row2->unit_price;
                $temp->pro_total_price[] = (double) $row2->pro_total_price;
                $temp->pro_remarks[] = $row2->pro_remarks;
				$temp->pro_name[] = $row2->pro_name;
				$temp->part_number[] = $row2->part_number;
				$temp->stamp_number[] = $row2->stamp_number;
				$temp->pro_description[] = $row2->pro_description;
				$temp->model_no[] = $row2->model_no;
            }
		}
        return $temp;
    }

    public function getAllEnquires() {
        $ret = array();
        $query = "Select *,IF((Select count(*) from enq_det_po where activestatus=1 and enq_detserialnumber_fk IN(Select enq_detserialnumber from enquiry_detail as ed where ed.activestatus=1 and ed.enq_serialnumber_fk=eh.enq_serialnumber)) > 0,1,0) as enq_edit from enquiry_header as eh where eh.activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoEnquiryHeader();
            $temp->enq_serialnumber = (int) $row->enq_serialnumber;
            $temp->enq_code = $row->enq_code;
			$temp->total_amount = (double) $row->total_amount;
            $temp->total_qty = (double) $row->total_qty;
			$temp->currency_serialnumber_fk = (int) $row->currency_serialnumber_fk;
			$temp->current_rate = (double) $row->current_rate;
			$temp->enq_date = date("d-m-Y", strtotime($row->enq_date));
            $temp->enq_remarks = $row->enq_remarks;
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
			$temp->enq_edit = (int) $row->enq_edit;
			
            $query1 = "select enq_sup_serialnumber,supplier_serialnumber_fk from enquiry_supplier where enq_serialnumber_fk='$row->enq_serialnumber'and activestatus=1";
            $s1 = mysql_query($query1);
            while ($row1 = mysql_fetch_object($s1)) {
                $temp->enq_sup_serialnumber[] = (int) $row1->enq_sup_serialnumber;
                $temp->supplier_serialnumber_fk[] = (int) $row1->supplier_serialnumber_fk;
            }
            $query2 = "select ed.enq_detserialnumber,ed.pro_serialnumber_fk,ed.unit_price,ed.pro_total_price,ed.pro_qty,ed.pro_remarks,ph.pro_name,ph.part_number,ph.stamp_number,
			ph.pro_description,ph.model_no from enquiry_detail as ed LEFT JOIN product_header as ph ON ph.serialnumber=ed.pro_serialnumber_fk and ph.activestatus=1 
			where ed.enq_serialnumber_fk='$row->enq_serialnumber' and ed.activestatus=1";
            $s2 = mysql_query($query2);
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->enq_detserialnumber[] = (int) $row2->enq_detserialnumber;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
                $temp->pro_qty[] = (double) $row2->pro_qty;
				$temp->unit_price[] = (double) $row2->unit_price;
                $temp->pro_total_price[] = (double) $row2->pro_total_price;
                $temp->pro_remarks[] = $row2->pro_remarks;
				$temp->pro_name[] = $row2->pro_name;
				$temp->part_number[] = $row2->part_number;
				$temp->stamp_number[] = $row2->stamp_number;
				$temp->pro_description[] = $row2->pro_description;
				$temp->model_no[] = $row2->model_no;
            }
            $ret[] = $temp;
            mysql_free_result($s1);
            mysql_free_result($s2);
        }
        mysql_free_result($s);
        return $ret;
    }

	public function addNewEnquiryHeader($VObject) {
        $s = new EnquiryDetails();
        $enqCode = $s->EnquiryAutoCode();
        $query = "insert into enquiry_header(enq_code,total_amount,total_qty,currency_serialnumber_fk,current_rate,enq_date,enq_remarks,cre_userid_fk,cre_datetime,activestatus) values ('$enqCode','$VObject->total_amount','$VObject->total_qty','$VObject->currency_serialnumber_fk','$VObject->current_rate','$VObject->enq_date','$VObject->enq_remarks','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) {
            $last_id = mysql_insert_id();
            foreach ($VObject->supplier_serialnumber_fk as $r => $value) {
                $query1 = "insert into enquiry_supplier(enq_serialnumber_fk,supplier_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus)values ('$last_id','$value','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                $s1 = mysql_query($query1) or die(mysql_error());
            }
            foreach ($VObject->pro_serialnumber_fk as $r => $value) {
                $pro_qty = mysql_real_escape_string($VObject->pro_qty[$r]);
				$unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
                $pro_total_price = mysql_real_escape_string($VObject->pro_total_price[$r]);
                $pro_remarks = mysql_real_escape_string($VObject->pro_remarks[$r]);
                $query2 = "insert into enquiry_detail(enq_serialnumber_fk,pro_serialnumber_fk,pro_qty,unit_price,pro_total_price,pro_remarks,cre_userid_fk,cre_datetime,activestatus) values('$last_id','$value','$pro_qty','$unit_price','$pro_total_price','$pro_remarks','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
        }

		return $enqCode;
       // return $this->getEnquiryByCode($last_id);
    }
	
    public function editEnquiry($VObject) {
        $query = "update enquiry_header set enq_code='$VObject->enq_code',total_amount='$VObject->total_amount',total_qty='$VObject->total_qty',currency_serialnumber_fk='$VObject->currency_serialnumber_fk',current_rate='$VObject->current_rate',enq_date='$VObject->enq_date',enq_remarks='$VObject->enq_remarks' where enq_serialnumber='$VObject->enq_serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("update enquiry_supplier set activestatus=0 where enq_serialnumber_fk='$VObject->enq_serialnumber'") or die(mysql_error());
            mysql_query("update enquiry_detail set activestatus=0 where enq_serialnumber_fk='$VObject->enq_serialnumber'") or die(mysql_error());
            foreach ($VObject->supplier_serialnumber_fk as $r => $value) {
                $query1 = "insert into enquiry_supplier(enq_serialnumber_fk,supplier_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus)values ('$VObject->enq_serialnumber','$value','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query1) or die(mysql_error());
            }
            foreach ($VObject->pro_serialnumber_fk as $r => $value) {
                $pro_qty = mysql_real_escape_string($VObject->pro_qty[$r]);
				$unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
                $pro_total_price = mysql_real_escape_string($VObject->pro_total_price[$r]);
                $pro_remarks = mysql_real_escape_string($VObject->pro_remarks[$r]);
                $query2 = "insert into enquiry_detail(enq_serialnumber_fk,pro_serialnumber_fk,pro_qty,unit_price,pro_total_price,pro_remarks,cre_userid_fk,cre_datetime,activestatus) values('$VObject->enq_serialnumber','$value','$pro_qty','$unit_price','$pro_total_price','$pro_remarks','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
        }
    }

    public function deleteEnquiry($serialnumber) {
        $query1 = ("update enquiry_supplier set activestatus=0 where enq_serialnumber_fk='$serialnumber'") or die(mysql_error());
        mysql_query($query1) or die(mysql_error());
        $query2 = ("update enquiry_detail set activestatus=0 where enq_serialnumber_fk='$serialnumber'") or die(mysql_error());
        mysql_query($query2) or die(mysql_error());
        $query3 = ("update enquiry_header set activestatus=0 where enq_serialnumber='$serialnumber'") or die(mysql_error());
        mysql_query($query3) or die(mysql_error());
        return $serialnumber;
    }

    public function enqSupplierData() {
        $ret = array();
        $query = "select * from enquiry_header where activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new voEnquirySupplier();
            $temp->enq_serialnumber = (int) $row->enq_serialnumber;
            $query1 = "select supplier_serialnumber_fk from enquiry_supplier where activestatus=1 and enq_serialnumber_fk='$row->enq_serialnumber'";
            $s1 = mysql_query($query1);
            while ($row1 = mysql_fetch_object($s1)) {
                $temp->supplier_serialnumber_fk[] = (int) $row1->supplier_serialnumber_fk;
            }
            $ret[] = $temp;
        }
        return $ret;
    }

    public function enqHeaderData() {
        $ret = array();
        $query1 = "select * from enquiry_header where activestatus=1";
        $s1 = mysql_query($query1);
        while ($row1 = mysql_fetch_object($s1)) {
            $temp = new voEnqHeaderDetails();
            $temp->enq_serialnumber[] = (int) $row1->enq_serialnumber;
            $temp->enq_code[] = $row1->enq_code;
            $ret[] = $temp;
        }

        return $ret;
    }

}

class voEnquirySupplier {

    var $enq_serialnumber;
    var $supplier_serialnumber_fk;

}

class voEnqHeaderDetails {

    var $enq_serialnumber;
    var $enq_code;

}

class voEnquiryEmail {

    //Values For Email
    var $enq_noEmail;
    var $supplierEmail;
    var $pro_nameEmail;
    var $prat_noEmail;
    var $model_noEmail;
    var $stamp_noEmail;
    var $quantityEmail;
    var $remarksEmail;
    var $baseRemarksEmail;

}

class VoEnquiryPopupList {
    var $enq_serialnumber;
    var $enq_code;
	var $total_qty;
    var $enq_remarks;
    var $isSelected;
}

class VoEnquiryHeader {
    var $enq_serialnumber;
    var $enq_code;
    var $enq_date;
    var $total_amount;
	var $total_qty;
    var $enq_remarks;
	var $enq_edit;
	var $currency_serialnumber_fk;
    var $current_rate;
    var $enq_sup_serialnumber;
    var $supplier_serialnumber_fk;
    var $enq_detserialnumber;
    var $pro_serialnumber_fk;
	var $pro_name;
	var $part_number;
	var $stamp_number;
	var $pro_description;
	var $model_no;
    var $pro_qty;
	var $unit_price;
    var $pro_total_price;
    var $pro_remarks;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
}

//$s = new EnquiryDetails();
//$s->addNewEnquiryHeader();
?>